package com.luhaoran.hrm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.luhaoran.hrm.entity.Employee;
import com.luhaoran.hrm.model.ApiEmployeePage;
import com.luhaoran.hrm.service.IEmployeeService;
import com.luhaoran.hrm.utils.ViewUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 员工相关接口
 */
@RestController
@RequestMapping("employee")
public class EmployeeController {


    @Resource
    HttpServletRequest request;
    @Resource
    HttpServletResponse response;
    @Resource
    IEmployeeService employeeService;

    /**
     * 员工列表
     * @return
     */
    @PostMapping
    public Object index(@RequestBody ApiEmployeePage apiEmployeePage) {
        apiEmployeePage.setPageSize(3);
        IPage<Employee> employeeIPage = employeeService.getPage(apiEmployeePage);
        return ViewUtil.json(response, 200, "success", employeeIPage);
    }

    /**
     * 获取单个员工
     * @return
     */
    @GetMapping("get")
    public Object get(@RequestParam("id") Integer id) {
        Employee employee = employeeService.getById(id);
        return ViewUtil.json(response, 200, "success", employee);
    }

    /**
     * 保存员工
     * @return
     */
    @PostMapping("save")
    public Object save(@RequestBody Employee employee) {
        employeeService.saveOrUpdate(employee);

        return ViewUtil.json(response, 200, "success");
    }


    /**
     * 移除员工
     * @param id
     * @return
     */
    @GetMapping("remove/{id}")
    public Object remove(@PathVariable("id") Integer id) {
        employeeService.removeById(id);
        return ViewUtil.json(response, 200, "success");
    }

}

